package com.shop.cereshop.app.controller.product;

import com.github.pagehelper.PageInfo;
import com.shop.cereshop.app.annotation.NoRepeatWebLog;
import com.shop.cereshop.app.dao.product.CereShopProductDAO;
import com.shop.cereshop.app.page.product.ProductDetail;
import com.shop.cereshop.app.page.product.ProductSkus;
import com.shop.cereshop.app.param.product.ProductParam;
import com.shop.cereshop.app.param.product.SkuParam;
import com.shop.cereshop.app.service.buyer.CereBuyerUserService;
import com.shop.cereshop.app.service.product.CereShopProductService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.product.CereShopProduct;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.utils.EmptyUtils;
import com.shop.cereshop.commons.utils.GsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 商品模块
 */
@RestController
@RequestMapping("product")
/**
 * 注解方式生成日志对象，指定topic生成对象类名
 */
@Slf4j(topic = "ProductController")
@CrossOrigin
@Api(value = "商品模块", tags = "商品模块")
public class ProductController {

    @Autowired
    private CereShopProductService cereShopProductService;

    @Autowired
    private CereBuyerUserService cereBuyerUserService;
    @Autowired
    private CereShopProductDAO cereShopProductDAO;

    /**
     * 商品详情查询
     *
     * @param param
     * @return
     */
    @GetMapping("getById")
    @ApiOperation(value = "商品详情查询")
    public Result<ProductDetail> getById(ProductParam param, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        ProductDetail detail = cereShopProductService.getById(param, user);
        detail.setSoldOut(detail.getNumber());
        Long productId = param.getProductId();
        CereShopProduct cereShopProduct = cereShopProductDAO.getByID2(productId);
        if (cereShopProduct.getProductNo() == null) {
            if (cereShopProduct.getCommodityProportion() == null) {
                cereShopProduct.setCommodityProportion(0);
            }
            BigDecimal price = cereShopProductDAO.getPrice(productId);
            //w分享赚的钱, sharePrice = 原价*分享比例/100
            BigDecimal sharePrice = price.multiply(new BigDecimal(cereShopProduct.getCommodityProportion())).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
            detail.setSharePrice(sharePrice);
        }else {
            Integer comm = cereShopProduct.getCommodityProportion();
            if (comm==null){
                comm=0;
            }
            detail.setSharePrice(new BigDecimal(comm));
        }
        return new Result(detail, CoReturnFormat.SUCCESS);
    }

    @GetMapping("talk")
    @ApiOperation(value = "商品详情查询")
    public Result<ProductDetail> talk(ProductParam param,
                                      @RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "row", defaultValue = "10") Integer row,
                                      HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        PageInfo<?> detail = cereShopProductService.talk(param, user, page, row);
        return new Result(detail, CoReturnFormat.SUCCESS);
    }

    /**
     * 删除搜索记录
     *
     * @return
     */
    @PostMapping("deleteAllSearch")
    @ApiOperation(value = "删除搜索记录")
    @NoRepeatWebLog
    public Result deleteAllSearch(HttpServletRequest request) throws CoBusinessException {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        String name = "";
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
            name = user.getWechatName();
        }
        cereShopProductService.deleteAllSearch(user);

        return new Result(CoReturnFormat.SUCCESS, name, "删除搜索记录", "");
    }

    /**
     * 选择商品查询
     *
     * @param param
     * @return
     */
    @GetMapping("getProducts")
    @ApiOperation(value = "选择商品查询")
    public Result<ProductSkus> getProducts(SkuParam param) throws Exception {
        ProductSkus products = cereShopProductService.getProducts(param);
        return new Result(products, CoReturnFormat.SUCCESS);
    }

    /**
     * 获取分享图片
     *
     * @param param
     * @return
     */
    @GetMapping("getSharePic")
    @ApiOperation(value = "获取分享图片")
    public Result<String> getSharePic(ProductParam param, HttpServletRequest request) throws CoBusinessException {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        CereShopProduct product = cereShopProductService.selectByPrimaryKey(param.getProductId());
        param.setClassifyId(product.getClassifyId());
        String sharePicUrl = cereShopProductService.getSharePic(param, user);
        Result<String> result = new Result();
        result.setCode(CoReturnFormat.SUCCESS);
        result.setData(sharePicUrl);
        param.setBuyerUserId(user.getBuyerUserId());
        result.setJson(GsonUtil.objectToGson(param));
        return result;
    }

}
